package ryulib.game;

import android.view.KeyEvent;
import android.view.MotionEvent;

public class GameControlGroup extends GameControlBase {

	public GameControlGroup(GameControlGroup gameControlGroup) {
		super(gameControlGroup);

	}

	protected GameControlList _GroupList = new GameControlList();
	
	public void clearControls() {
		_GroupList.deleteAll();
	}
	
	public void addControl(GameControlBase control) {
		control._GameControlGroup = this;
		control._GameControlList  = this._GroupList; 
		_GroupList.addQue(control);
	}

	public void removeControl(GameControlBase control) {
		control.delete();
		control._GameControlGroup = null;
		control._GameControlList  = null; 
	}

	private boolean _IsStarted = false;
	
	void onRepeate(GamePlatformInfo platformInfo) {
		if (getDeleted()) return; 
			
		if (_IsStarted == false) {
			_IsStarted = true;
			onStart(platformInfo);
		}
			
		if (getEnabled()) {
			onTick(platformInfo);
			
			if (getVisible()) {
				onDraw(platformInfo);
				_GroupList.onRepeate(platformInfo);
			}
		}
	}
	
	protected boolean doKeyDown(GamePlatformInfo platformInfo, int keyCode, KeyEvent msg) {
		return false;
	}
	
	@Override
	final protected boolean onKeyDown(GamePlatformInfo platformInfo, int keyCode, KeyEvent msg) {
		if (_GroupList.onKeyDown(platformInfo, keyCode, msg) == false) return doKeyDown(platformInfo, keyCode, msg);
		return true;
    }

	protected boolean doKeyUp(GamePlatformInfo platformInfo, int keyCode, KeyEvent msg) {
		return false;
	}

	@Override
	final protected boolean onKeyUp(GamePlatformInfo platformInfo, int keyCode, KeyEvent msg) {		
    	if (_GroupList.onKeyUp(platformInfo, keyCode, msg) == false) return doKeyUp(platformInfo, keyCode, msg);
		return true;
    }

	protected boolean doTouchEvent(GamePlatformInfo platformInfo, MotionEvent event) {
		return false;
	}
	
	@Override
	final protected boolean onTouchEvent(GamePlatformInfo platformInfo, MotionEvent event) {
    	if (_GroupList.onTouchEvent(platformInfo, event) == false) return doTouchEvent(platformInfo, event);
		return true;
    }
	
	void ArrangeControls() {
		_GroupList.arrangeControls();
	}
}
